package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.ShoppingCartOrderDetailsConvert;
import com.starlink.model.vo.mall.MallOrderCarCancelListVO;
import com.starlink.model.vo.mall.MallOrderDetailListVO;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.ShoppingCartOrderDetailsDtoMapper;
import com.starlink.model.domain.ShoppingCartOrderDetailsDO;
import com.starlink.mapper.ShoppingCartOrderDetailsMapper;
import com.starlink.model.request.ShoppingCartOrderDetailsRequest;
import com.starlink.service.ShoppingCartOrderDetailsService;
import com.starlink.model.vo.ShoppingCartOrderDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.common.enums.OrderTypeEnum.*;
import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;
import java.util.Map;

/**
 * @author zhanghong
 */
@Service
public class ShoppingCartOrderDetailsServiceImpl extends ServiceImpl<ShoppingCartOrderDetailsMapper, ShoppingCartOrderDetailsDO> implements ShoppingCartOrderDetailsService {
    @Autowired
    private ShoppingCartOrderDetailsMapper shoppingCartOrderDetailsMapper;

    @Override
    public ShoppingCartOrderDetailsVO getByKey(long id) {
        ShoppingCartOrderDetailsVO shoppingCartOrderDetailsVO = ShoppingCartOrderDetailsDtoMapper.INSTANCE.do2VO(getById(id));
        return shoppingCartOrderDetailsVO;
    }

    @Override
    public PageInfo<ShoppingCartOrderDetailsVO> pageQuery(Pageable pageable,ShoppingCartOrderDetailsRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<ShoppingCartOrderDetailsDO> list = shoppingCartOrderDetailsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ShoppingCartOrderDetailsVO> shoppingCartOrderDetailsVOList = ShoppingCartOrderDetailsConvert.buildShoppingCartOrderDetailsVoList(list);
        return new PageInfo(shoppingCartOrderDetailsVOList);
    }

    @Override
    public List<ShoppingCartOrderDetailsVO> listQuery(ShoppingCartOrderDetailsRequest request) {
        List<ShoppingCartOrderDetailsDO> list = shoppingCartOrderDetailsMapper.selectDOList(ShoppingCartOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        List<ShoppingCartOrderDetailsVO> shoppingCartOrderDetailsVOS = ShoppingCartOrderDetailsConvert.buildShoppingCartOrderDetailsVoList(list);
        return shoppingCartOrderDetailsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(ShoppingCartOrderDetailsRequest request) {
        request.setId(getId());
        save(ShoppingCartOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(ShoppingCartOrderDetailsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(ShoppingCartOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        shoppingCartOrderDetailsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<ShoppingCartOrderDetailsDO> carOdersList) {
        shoppingCartOrderDetailsMapper.batchInsertDO(carOdersList);
    }

    @Override
    public List<Map<String, Long>> selectQuantity(Long orderId,List<Long> carOrderIds) {
        return shoppingCartOrderDetailsMapper.selectQuantity(orderId,carOrderIds);
    }

    @Override
    public List<MallOrderCarCancelListVO> selectSpecificationId(Long orderId) {
        return shoppingCartOrderDetailsMapper.selectSpecificationId(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCarOrderDetaile(Long orderId) {
        shoppingCartOrderDetailsMapper.deleteCarOrderDetaile(orderId);
    }

    @Override
    public MallOrderDetailListVO selectCarOrderDetail(Long orderId) {
        return shoppingCartOrderDetailsMapper.selectCarOrderDetail(orderId);
    }

    @Override
    public MallOrderCarCancelListVO selectDataInfoByAId(Long key, Long orderId) {
        return shoppingCartOrderDetailsMapper.selectDataInfoByAId(key,orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIsRefunds(Long orderId, Long articlesId) {
        shoppingCartOrderDetailsMapper.updateIsRefunds(orderId,articlesId);
    }

    @Override
    public List<MallOrderCarCancelListVO> selectSpecificationIdByOrderId(Long orderId) {
        return shoppingCartOrderDetailsMapper.selectSpecificationIdByOrderId(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByOrderIds(String stringIds) {
        shoppingCartOrderDetailsMapper.deletedByOrderIds(Convert.toStrArray(stringIds));
    }
}
